package com.axin.blog.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.axin.blog.api.domain.Blog;
import com.axin.blog.api.domain.BlogCategory;
import com.axin.blog.mapper.BlogCategoryMapper;
import com.axin.blog.service.IBlogCategoryService;
import com.axin.blog.service.IBlogService;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.core.exception.ServiceException;
import com.axin.common.mybatis.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 博客分类 服务层实现
 *
 * @Author axin
 * @Date 2021/10/29 17:08
 **/
@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory> implements IBlogCategoryService {

    @Autowired
    private IBlogService blogService;

    /**
     * 根据参数分页查询博客分类列表
     *
     * @param blogCategory
     * @return
     */
    @Override
    public TableDataInfo selectPageBlogCategoryList(BlogCategory blogCategory) {
        return PageUtils.buildDataInfo(baseMapper.selectPageBlogCategoryList(PageUtils.buildPage(), blogCategory));
    }


    /**
     * 根据条件查询博客分类列表
     *
     * @param blogCategory
     * @return
     */
    @Override
    public List<BlogCategory> selectBlogCategoryList(BlogCategory blogCategory) {
        return baseMapper.selectBlogCategoryList(blogCategory);
    }


    /**
     * 新增博客分类
     *
     * @param category
     * @return 结果
     */
    @Override
    public int insertBlogCategory(BlogCategory category) {
        return baseMapper.insert(category);
    }


    /**
     * 查询分类信息
     *
     * @param categoryId
     * @return
     */
    @Override
    public BlogCategory selectBlogCategoryById(Long categoryId) {
        return baseMapper.selectById(categoryId);
    }

    /**
     * 修改分类信息
     *
     * @param category 分类信息
     * @return
     */
    @Override
    public int updateBlogCategory(BlogCategory category) {
        return baseMapper.updateById(category);
    }

    /**
     * 批量删除分类
     *
     * @param categoryIds 需要删除的参数ID
     * @return 结果
     */
    @Override
    public void deleteCategoryByIds(Long[] categoryIds) {
        for (Long categoryId : categoryIds) {
            BlogCategory category = selectBlogCategoryById(categoryId);
            // 判断分类下是否有博客
            long count = blogService.count(new LambdaQueryWrapper<Blog>().eq(Blog::getCategoryId, category.getCategoryId()));
            if (count > 0) {
                throw new ServiceException("此分类下存在博客，不能删除");
            }
        }
        baseMapper.deleteBatchIds(Arrays.asList(categoryIds));
    }


    /**
     * 检验分类名称是否唯一
     *
     * @param category
     * @return
     */
    @Override
    public boolean checkCategoryNameUnique(BlogCategory category) {
        Long configId = ObjectUtil.isNull(category.getCategoryId()) ? -1L : (category.getCategoryId());
        BlogCategory info = baseMapper.selectOne(new LambdaQueryWrapper<BlogCategory>().eq(BlogCategory::getCategoryName, category.getCategoryName()));
        if (ObjectUtil.isNotNull(info) && info.getCategoryId().longValue() != configId.longValue()) {
            return false;
        }
        return true;
    }


    /***********************************************************前台接口**********************************************/

    /**
     * 获取分类归档
     * @return
     */

    @Override
    public List<BlogCategory> getCategoryArchiveList() {

        // 查询开启的分类
        LambdaQueryWrapper<BlogCategory> lqw = new LambdaQueryWrapper<BlogCategory>()
                .eq(BlogCategory::getStatus, 1)
                .orderByAsc(BlogCategory::getSortNum);
        List<BlogCategory> categoryList = baseMapper.selectList(lqw);

        // 统计分类下的博客数量
        categoryList.forEach(item ->
                item.setCountNum(blogService.count(new LambdaQueryWrapper<Blog>().eq(Blog::getCategoryId, item.getCategoryId())))
        );

        // 按照博客数量降序
        categoryList.sort(Comparator.comparing(BlogCategory::getCountNum).reversed());

        return categoryList;
    }


}
